const { request } = require('../../../../services/request')
Component({

    /**
     * 组件的属性列表
     */
    properties: {
        userInfo: Object,
        refresh: Object,
        fromMenu: Boolean,
    },

    /**
     * 组件的初始数据
     */
    data: {
        visible: false,
        // avatar: JSON.parse(wx.getStorageSync('userInfo'))
    },

    /**
     * 组件的方法列表
     */
    methods: {
        handleEdit(e) {
            wx.navigateTo({
                url: `/pages/edit-user/index?type=edit&id=${e.target.dataset.id}`,
            })
        },
        handleDel() {
            this.setData({
                visible: true,
            })
        },
        refresh() {
            this.triggerEvent('refresh')
        },
        handleSetDefault(e) {
          // this.triggerEvent('selectUser', params)
          //       if (this.data.userInfo.isDefault === 0) {
          //           request('POST', `/applet/answer/user/setDefault`, {
          //                   answeUserId: this.data.userInfo.answeUserId,
          //               })
          //               .then(res => {
          //                   this.refresh()
          //                   let params = {
          //                     answeUserId: this.data.userInfo.answeUserId,
          //                     userId: this.data.userInfo.userId,
          //                   }
                          
          //               })
          //               .catch()
          //       } else {
          //           wx.navigateBack({
          //               url: '/pages/my/index',
          //           })
          //       }
                let params = {
                    answeUserId: this.data.userInfo.answeUserId,
                    userId: this.data.userInfo.userId,
                }
                this.triggerEvent('selectUser', params)
            
        },
        handleCancel() {
            this.setData({
                visible: false,
            })
        },
        handleDialogTap(e){
console.log(e)
if(e.detail.index==0){
  this.setData({visible:false})
}else{
  this.handleConfirm()
}
        },
        handleConfirm() {
            request(
                    'POST',
                    `/applet/answer/user/delete/${this.data.userInfo.answeUserId}`
                )
                .then((res) => {
                    this.setData({
                        visible: false,
                    })
                    this.refresh()
                })
                .catch(err => {
                    wx.showToast({
                        title: err.msg,
                        icon: 'error',
                    })
                    this.setData({
                        visible: false,
                    })
                })
        },
    },
})